package com.zx.auth.block.controller;

import com.zx.auth.block.param.OAuth2AuthorizationConsentParam;
import com.zx.auth.block.service.IOAuth2AuthorizationConsentService;
import com.zx.auth.block.vo.OAuth2AuthorizationConsentVO;
import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.log.annotation.OperateLog;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * OAuth2授权同意管理控制器
 * 提供OAuth2授权同意信息的查询和管理功能
 */
@RestController
@RequestMapping("/oauth2/consent")
@RequiredArgsConstructor
public class OAuth2AuthorizationConsentController {

    private final IOAuth2AuthorizationConsentService oAuth2AuthorizationConsentService;

    /**
     * 根据客户端ID和主体名称获取OAuth2授权同意信息
     *
     * @param registeredClientId 客户端ID
     * @param principalName      主体名称
     * @return 授权同意信息
     */
    @PreAuthorize("hasAuthority('oauth2:consent:info')")
    @OperateLog(value = "获取授权同意信息")
    @GetMapping("/info/{registeredClientId}/{principalName}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "客户端ID", rules = {ValidateRulesEnum.NOT_NULL}),
            @ParameterValidation(index = 1, desc = "主体名称", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ObjectWrapper<OAuth2AuthorizationConsentVO> getAuthorizationConsent(
            @PathVariable String registeredClientId,
            @PathVariable String principalName) {
        return oAuth2AuthorizationConsentService.getAuthorizationConsent(registeredClientId, principalName);
    }

    /**
     * 分页查询OAuth2授权同意信息列表
     *
     * @param consentParam 授权同意参数
     * @return 授权同意信息列表
     */
    @PreAuthorize("hasAuthority('oauth2:consent:list')")
    @OperateLog(value = "分页查询授权同意")
    @PostMapping("/list")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "授权同意参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Query.class})
    )
    public PageWrapper<OAuth2AuthorizationConsentVO> listAuthorizationConsents(@RequestBody OAuth2AuthorizationConsentParam consentParam) {
        return oAuth2AuthorizationConsentService.listAuthorizationConsents(consentParam);
    }

    /**
     * 删除OAuth2授权同意信息
     *
     * @param registeredClientId 客户端ID
     * @param principalName      主体名称
     * @return 删除结果
     */
    @PreAuthorize("hasAuthority('oauth2:consent:remove')")
    @OperateLog(value = "删除授权同意")
    @DeleteMapping("/deleteConsent/{registeredClientId}/{principalName}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "客户端ID", rules = {ValidateRulesEnum.NOT_NULL}),
            @ParameterValidation(index = 1, desc = "主体名称", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ResponseWrapper deleteAuthorizationConsent(
            @PathVariable String registeredClientId,
            @PathVariable String principalName) {
        return oAuth2AuthorizationConsentService.deleteAuthorizationConsent(registeredClientId, principalName);
    }
}